#pragma once

#include "FileManager.h"

void image_parse::CFileList::myInit(
	string strFN_TitleList
	, string strPN_SourceImageList, string strFN_Sfx_SourceImage
	, string strPN_GroundTruthList, string strFN_Sfx_GroundTruth
	)
{
	int i;
	ifstream inFile(strFN_TitleList.c_str());

	inFile>>FileAmount;
	TitleList.Resize(FileAmount);
	SourceImageList.Resize(FileAmount);
	GroundTruthList.Resize(FileAmount);
	for ( i = 0; i < FileAmount; ++i )
	{
		inFile>>TitleList.pElement[i];
		SourceImageList.pElement[i] = strPN_SourceImageList + TitleList.pElement[i] + strFN_Sfx_SourceImage;
		GroundTruthList.pElement[i] = strPN_GroundTruthList + TitleList.pElement[i] + strFN_Sfx_GroundTruth;
	}

	inFile.clear();
	inFile.close();
}

void image_parse::CFileEntity::myInit(const CFileList& FileList, int FileIndex, const CClassInfoSet& ClassSet)
{
	if (FileIndex<0 || FileIndex>=FileList.FileAmount) return;
	myInit(FileList.SourceImageList.pElement[FileIndex], FileList.GroundTruthList.pElement[FileIndex], ClassSet);
}

void image_parse::CFileEntity::myInit(string strFN_SourceImage, string strFN_GroundTruthImage, const CClassInfoSet& ClassSet)
{
	int i,j;

	CExternalTools::myLoadImage(strFN_SourceImage, SourceImage);
	W = SourceImage.dimx();
	H = SourceImage.dimy();
	WH = W * H;

	CExternalTools::myLoadImage(strFN_GroundTruthImage, GroundTruthImage);
	//CScalableImage ScalableImage;
	//CScalableImage::CScaleConfig ScaleConfig;
	//ScalableImage.myInit(GroundTruthImage);
	//ScalableImage.BuildScaleConfig_i(ScaleConfig, W, H);
	//ScalableImage.BuildImage(GroundTruthImage, ScaleConfig);

	LabelImage.assign(W, H);
	int* pl = LabelImage.data;
	const int* qr = GroundTruthImage.data + W * H * 1 * 0;
	const int* qg = GroundTruthImage.data + W * H * 1 * 1;
	const int* qb = GroundTruthImage.data + W * H * 1 * 2;
	for ( i = 0; i < H; ++i ) for ( j = 0; j < W; ++j )
		*(pl++) = ClassSet.GetLabel(*(qr++), *(qg++), *(qb++));
}

void image_parse::CFileEntity::DoTRS(int NewW, int NewH, const C_TRSable_Image::CParameters& TRS_Config, bool fTiling)
{
	C_TRSable_Image TRSor;
	
	TRSor.myInit(SourceImage);
	TRSor.BuildImage(SourceImage, NewW, NewH, TRS_Config, fTiling);
	TRSor.myInit(GroundTruthImage);
	TRSor.BuildImage(GroundTruthImage, NewW, NewH, TRS_Config, fTiling);
	TRSor.myInit(LabelImage);
	TRSor.BuildImage(LabelImage, NewW, NewH, TRS_Config, fTiling);
	W = NewW; H = NewH; WH = W * H;
}

